if( window.attachEvent ) {window.attachEvent( 'onload', InitAll );}
else if( window.addEventListener ) {window.addEventListener( 'load', InitAll, false );}
else {document.addEventListener( 'load', InitAll, false );}

function InitAll() {
    availableTags = ( typeof availableTags != 'undefined') ? availableTags : [];
    $( '.datepicker' ).datetimepicker();
    $( '#accordion' ).accordion();
    $( "#subiecte_interes" ).autocomplete( {
        source: availableTags
    } );

    $( ".mark-check" ).click( function() {
        var dataRef = $( this ).attr( 'data-ref' );
        var userType = dataRef.split( "-" )[0];
        var etapa = dataRef.split( "-" )[1];
        var id = dataRef.split( "-" )[2];
        var valoare = (parseInt( $( this ).val() ) + 1) % 2;
        raspuns = toggleCheckEtapa( userType, etapa, id, valoare );
        if( raspuns != - 1 ) {
            $( this ).val( valoare );
            switch( parseInt( raspuns ) ) {
                case 1:
                case 2:
                    if ($(this).hasClass('stage_check'))
                        $(this).parent().find('i').attr('class','fa fa-check-square-o');
                        else
                        $(this).parent().find('i').attr('class','fa fa-check-circle-o check_profesor');
                    //$( this ).removeClass( "check_student unchecked" ).addClass( "check_profesor" );
                    break;
                case 3:
                    $(this).parent().find('i').attr('class','fa fa-check-circle-o');
                    //$( this ).removeClass( "check_profesor unchecked" ).addClass( "check_student" );
                    break;
                default:
                    if ($(this).hasClass('stage_check'))
                        $(this).parent().find('i').attr('class','fa fa-square-o');
                        else
                        $(this).parent().find('i').attr('class','fa fa-circle-o');
                    //$( this ).removeClass( "check_student check_profesor" ).addClass( "unchecked" );
                    break;
            }
        }
        else {
            $( this ).attr( "checked", ((valoare + 1) % 2) ? true : false );
        }
    } );
    if ($('.commit_alert').length)
        window.setTimeout(function() {
            $('.commit_alert').addClass('animated');
            $('.commit_alert').addClass('bounce');
        },500);
}

if( req_nr == undefined ) {
    var req_nr = 0;
}

function AddRequirement() {
    var content = '<div class="requirement_add_form"><h1>Cerinta ' + (req_nr + 1) + '</h1>' +
            '<div class="form-group"><div class="input select"><label for="Requirement' + req_nr +
            'Type">Tip</label><select name="data[Requirement' + req_nr +
            '][type]" class="form-control" onchange="ReqSelect(this)" id="Requirement' + req_nr + 'Type">' +
            '<option value="text">Text</option>' + '<option value="select">Select</option>' +
            '<option value="radio">Radio</option>' + '<option value="checkbox">Check</option>' +
            '</select></div></div>' +
            '<div class="form-group req_hidden"><div class="input text"><label for="Requirement' + req_nr +
            'TypeValues">Valori</label><input name="data[Requirement' + req_nr +
            '][type_values]" class="form-control" type="text" id="Requirement' + req_nr + 'TypeValues"></div></div>' +
            '<div class="form-group"><div class="input checkbox"><label for="Requirement' + req_nr +
            'Name">Nume</label><input name="data[Requirement' + req_nr +
            '][required]" class="form-control" type="checkbox" id="Requirement' + req_nr + 'Required"></div></div>' +
            '<div class="form-group"><div class="input text"><label for="Requirement' + req_nr +
            'Name">Nume</label><input name="data[Requirement' + req_nr +
            '][name]" class="form-control" type="text" id="Requirement' + req_nr + 'Name"></div></div>' +
            '<div class="form-group"><div class="input text"><label for="Requirement' + req_nr +
            'Description">Descriere</label><input name="data[Requirement' + req_nr +
            '][description]" class="form-control" type="text" id="Requirement' + req_nr + 'Description"></div></div>' +
            '</div>';
    $( '.added_requirements' ).append( content );
    req_nr ++;
}


if( task_nr == undefined ) {
    var task_nr = 0;
}

function AddTask() {
    var content = '<div class="requirement_add_form"><h1>Task ' + (task_nr + 1) + '</h1>' +
            '<div class="form-group"><div class="input text"><label for="Task' + task_nr +
            'Name">Nume</label><input name="data[Task' + task_nr +
            '][name]" class="form-control" type="text" id="Task' + task_nr + 'Name"></div></div>' + '</div>';
    $( '.added_tasks' ).append( content );
    task_nr ++;
}


function DeleteReq( id ) {
    $.post( SITE_URL + 'ajax/stergere_cerinta/' + id );
    $( '.req' + id ).hide( 500 );
}

function DeleteTask( id ) {
    $.post( SITE_URL + 'ajax/stergere_task/' + id );
    $( '.req' + id ).hide( 500 );
}


function check_student( id, e ) {
    $.ajax( {
        type: "GET",
        url: SITE_URL + 'ajax/check_student/' + id,
        success: function() {
            alert( 'bine' );
            $( this ).addClass( 'checked-student' );
        },
        error: (function() {alert( 'eroare' );})
    } );
}


function ReqSelect( sel ) {
    var v = $( sel ).val();
    if( v == 'text' ) {
        var p = $( sel ).parent().parent().parent();
        var c = $( p ).find( '.form-group' )[1];
        $( c ).hide();
    }
    else {
        var p = $( sel ).parent().parent().parent();
        var c = $( p ).find( '.form-group' )[1];
        $( c ).show();
    }

}

function toggleCheckEtapa( userType, etapa, id, valoare ) {
    //userType = 1 -> profesor
    //userType = 2 -> student
    if( typeof userType == 'undefined' ) {
        userType = 1;
    }
    var suburl = "check_stage_";
    if( etapa == 0 ) {
        suburl = 'check_task_';
    }
    raspuns = 1;
    $.ajax( {
        "type": "GET",
        "async": false,
        "url": SITE_URL + "ajax/" + suburl + ((userType == 1) ? "profesor" : "student" ) + "/" + id + "/" + valoare,
        "success": function( response ) {
            raspuns = response;
        },
        "error": function( code, reason ) {
            raspuns = - 1;
        } } );
    return raspuns;
}


